setScreenMetrics(device.width, device.height);

app_func = {
    appName: "番茄免费小说",
    packageName: "com.dragon.read",
    mainActivity: "com.dragon.read.pages.main.MainFragmentActivity",
    bounds: {
        withdraw_button: null,
        watch_video_button: null,
        close_adv_button: null,
        goto_book_button: null,
        book_button: null,
        book_shop_button: null,
        task_button: null
    }
}

let current_package = "com.dragon.read";
function launchAppEnhance(app_name) {
    let result;
    let thread = threads.start(function () {
        toastLog("检查是否有打开权限!");
        while (true) {
            var allow = text("允许").findOne(5000);
            if (allow) {
                sleep(1000)
                toastLog("发现允许授权!");
                allow.click();
                sleep(1000);
                thread.interrupt();
                break;
            }
        }
    });
    sleep(1000);
    if (currentPackage() != current_package) {
        result = launchApp(app_name);
        sleep(3000);
    }
    else {
        result = currentPackage();
    }
    thread.interrupt();
    return result;
}

function get_app_info() {
    console.log("开始获取app信息------------------:", app_func.appName);

    console.log("当前软件版本号:", app.versionCode);
    console.log("当前软件版名称:", app.verionName);
    app_func.packageName = app.getPackageName(app_func.appName)
    console.log("获取应用名称对应的已安装的应用的包名:", app_func.packageName);
    console.log("获取应用包名对应的已安装的应用的名称:", app.getAppName(app_func.packageName));
}

function get_globals_info() {
    console.log("开始获取全局变量信息------------------:");
    console.log("当前正在运行的应用的包名:", currentPackage());
    console.log("当前正在运行的Activity的名称:", currentActivity());
    console.log("系统剪贴板的内容:", getClip());
    console.log("一个android.content.Context对象:", context);
}

function find_withdraw_button() {
    console.log("开始寻找提现按钮------------------:");
    var withdraw_button = text("立即提现").findOne(5000);
    if (withdraw_button) {
        if (withdraw_button.className() == "android.widget.Button")
            console.log("成功找到提现控件:", withdraw_button.bounds());
        console.log("控件className:", withdraw_button.className());
        console.log("控件text:", withdraw_button.text());
        app_func.bounds.withdraw_button = withdraw_button.bounds()
        return 0
    }
    else {
        toastLog("没找到提现控件");
        return -1
    }
}

function find_task_button() {
    var sucflag = 0
    //console.log("开始寻找任务按钮------------------:");
    var task_button = text("福利").findOne(5000);
    for (var i = 0; i < 3; i++) {
        if (task_button) {
            // console.log("成功找到任务按钮:",task_button.bounds() );
            // console.log("控件className:",task_button.className() );
            toastLog("控件text:", task_button.text());
            app_func.bounds.task_button = task_button.bounds()
            sucflag = 1;
            break;
        }
        else {
            toastLog("没找到任务界面");
            // 关广告  各种返回到首页
            clean_obs()
            continue;
        }
    }
    if (sucflag == 0) {
        return -1;
    }

    return 0;
}

function find_book_button() {
    var sucflag = 0
    //console.log("开始寻找任务按钮------------------:");
    var book_button = text("书架").findOne(5000);
    for (var i = 0; i < 3; i++) {
        if (book_button) {
            // console.log("成功找到任务按钮:",book_button.bounds() );
            // console.log("控件className:",book_button.className() );
            toastLog("控件text:", book_button.text());
            app_func.bounds.book_button = book_button.bounds()
            sucflag = 1;
            break;
        }
        else {
            toastLog("没找到书架界面");
            // 关广告  各种返回到首页
            close_all_ad()
            continue;
        }
    }
    if (sucflag == 0) {
        return -1;
    }

    return 0;
}

function find_book_shop_button() {
    var sucflag = 0
    //console.log("开始寻找任务按钮------------------:");
    var book_shop_button = text("书城").findOne(5000);
    for (var i = 0; i < 3; i++) {
        if (book_shop_button) {
            // console.log("成功找到任务按钮:",book_shop_button.bounds() );
            // console.log("控件className:",book_shop_button.className() );
            toastLog("控件text:", book_shop_button.text());
            app_func.bounds.book_shop_button = book_shop_button.bounds()
            sucflag = 1;
            break;
        }
        else {
            toastLog("没找到书城界面");
            // 关广告  各种返回到首页
            close_all_ad()
            continue;
        }
    }
    if (sucflag == 0) {
        return -1;
    }

    return 0;
}

//关闭各种弹窗广告
function close_all_ad() {
    try { textStartsWith('今日任务完成了').findOne(500).click(); } catch (e) { }
    try { text('我知道了').findOne(500).click(); } catch (e) { }
    try { text('跳过').findOne(500).click(); } catch (e) { }
    try { text('禁止').findOne(500).click(); } catch (e) { }
    try { id("tt_video_ad_close").findOne(500).click(); } catch (e) { }
    try {
        if (id("qn").findOne(500).click()) {
            toastLog("qn")
        }
    } catch (e) { }
}

//清除各种障碍 直到主界面
function clean_obs() {
    var result = null

    if (is_mainAct()) {
        //关闭各种广告
        close_all_ad()
        result = 0
    }
    else {
        //存在是在主页 也有弹窗
        result = err_back()
    }
    return result
}

function is_mainAct() {
    sleep(1000)
    var curact = currentActivity();
    if (curact != app_func.mainActivity) {
        //    toastLog("不在主界面")
        return false
    }
    else {
        //    toastLog("当前是主界面")
        return true
    }
}

function is_bookAct() {
    sleep(1000)
    var curact = currentActivity();
    if (curact != "com.dragon.read.reader.ReaderActivity") {
        //    toastLog("不在读书界面")
        return false
    }
    else {
        //    toastLog("当前是主界面")
        return true
    }
}

function err_back() {
    var curact = null;
    for (var i = 0; i < 3; i++) {
        curact = currentActivity()
        sleep(1000)
        if (curact != app_func.mainActivity) {
            toastLog("尝试回到主界面")
            back();
            sleep(3000)
        }
        else {
            toastLog("已回到当前主界面")
            break
        }
    }

    if (curact != app_func.mainActivity) {
        toastLog("回主界面失败")
        return -1
    }

    return 0
}

function find_watch_video_button() {
    toastLog("看视频奖励:");
    var target_button = textStartsWith("看视频").findOne(4000);
    if (target_button) {
        toastLog("控件text:", target_button.text());
        app_func.bounds.watch_video_button = target_button.bounds()
        return 0;
    }
    else {
        toastLog("没找到看视频奖励按钮");
        return -1;
    }
}

function find_goto_book_button() {
    console.log("开始寻找加入书架按钮------------------:");
    var target_button = text("加入书架").findOne(4000);
    if (target_button) {
        console.log("成功找到目标按钮:", target_button.bounds());
        console.log("控件className:", target_button.className());
        console.log("控件text:", target_button.text());
        app_func.bounds.goto_book_button = target_button.bounds()
        return 0;
    }
    else {
        toastLog("没找到关闭广告按钮");
        return -1;
    }
}

function find_button_by_text(textstr) {
    toastLog("寻找按钮:" + textstr);
    var task_button = null
    for (var i = 0; i < 2; i++) {
        task_button = text(textstr).findOne(3000);
        if (task_button) {
            console.log("找到控件text:", task_button.text());
            break;
        }
        else {
            console.log("第" + (i + 1) + "次未找到");
            sleep(1000)
            continue;
        }
    }

    return task_button
}

function find_button_by_textStartsWith(textstr) {
    toastLog("寻找按钮:" + textstr);
    var task_button = null
    for (var i = 0; i < 2; i++) {
        task_button = textStartsWith(textstr).findOne(3000);
        if (task_button) {
            console.log("找到控件text:", task_button.text());
            break;
        }
        else {
            console.log("第" + (i + 1) + "次未找到");
            sleep(1000)
            continue;
        }
    }
    sleep(1000)
    return task_button
}

//传入坐标参数
function click_button(bounds) {
    toastLog("点击按钮:" + bounds.centerX() + bounds.centerY());
    sleep(1000);
    click(bounds.centerX(), bounds.centerY());
    sleep(2000)
}

function open_baoxiang() {
    var baoxiang = text("开宝箱得金币").findOne(5000);
    if (baoxiang) {
        toastLog("开始点击开宝箱");
        sleep(1000);
        click_button(baoxiang.bounds());
        sleep(5000);
        return 0
    }
    else {
        toastLog("没有发现开宝箱得金币");
        return -1
    }
}

function handle_goto_book_task() {
    toastLog("开始做上书架任务");
    var suctimes = 0;
    //先接任务找到福利按钮
    find_task_button()
    click_button(app_func.bounds.task_button)
    //寻找加入书架按钮
    var parent = null;
    var targetButton = null;
    for (var i = 0; i < 5; i++) {
        targetButton = auto_find_button("加入书架")
        if (targetButton) {
            parent = targetButton.parent().parent();
            var tb = parent.child(1).child(0);
            toastLog("加入书架任务状态" + tb.text());
            //for test
            //if(false){}

            if (tb.text() == "已完成") {
                return 0
            }
            else {
                //开始处理任务
                //先删书再上架书
                handle_del_book()
                sleep(2000)
                //点击书城按钮
                var 书城 = find_button_by_text("书城")
                click_button(书城.bounds())
                if (书城) {
                    //选择书点击
                    try {
                        toastLog("开始选书上架...");
                        var book = className("android.widget.RelativeLayout").depth(20).findOne(5000)
                        if (book) {
                            toastLog("书已找到");
                            // var parentbook = book.parent()
                            click_button(book.bounds())
                        }
                    } catch (error) {
                        toastLog(error);
                        return -1
                    }
                    //寻找加入书架按钮
                    sleep(2000)
                    var jiaru = text("加入书架").findOne(2000)
                    if (jiaru) {
                        jiaru.click()
                        sleep(3000)
                        //返回
                        err_back()
                    }
                    else {
                        //已加入书架 或者广告
                        var yijiaru = text("已加入书架").findOne(2000)
                        if (yijiaru) {
                            //先返回APP 到书架->编辑->选中一本书->删除
                            err_back()
                        }
                        else {
                            toastLog("未找到操作按钮")
                            return -1
                        }
                    }
                }
                else {
                    toastLog("没找到书城按钮")
                    return -1
                }
            }
        }
        else {
            toastLog("没有找到任务入口")
            return -1
        }
    }

    if (suctimes == 0) {
        return -1;
    }

    return 0;
}

function handle_listen_book_task() {
    toastLog("开始做听书任务");
    //进入书城 点击一本书 点击听书按钮

    //此任务搁浅，听书有免费时限
}

//从书架删除书
function handle_del_book() {
    var 书架 = find_button_by_text("书架")
    if (书架) {
        click_button(书架.bounds())
    }
    else {
        clean_obs()
    }

    toastLog("开始删除书架");
    //先确定有书再执行
    var book = textContains("章").findOne(5000);
    if (book) {
        toastLog("有书")
    }
    else {
        toastLog("没有书,直接返回")
        return 0
    }

    //进入书架 点击编辑 选中所有书 点击删除
    var edit_button = text("编辑").findOne(4000);
    if (edit_button) {
        edit_button.click();
        全选 = text("全选").findOne(2000)
        if (全选) {
            全选.click()
            //点击删除
            var del_button = textStartsWith("删除").findOne(2000);
            sleep(2000)
            if (del_button) {
                click_button(del_button.bounds())
                sleep(2000)
                var queren = text("确认").findOne(2000);
                if (queren) {
                    queren.click()
                    toastLog("删除成功")
                }
                else {
                    toastLog("未找到确认按钮")
                }
            }
            else {
                toastLog("未找到删除按钮")
            }

        }
        // var checkboxlist = className("android.widget.CheckBox").find(5000);
        // if( checkboxlist.length > 0 ){
        //     toastLog( "找到" + checkboxlist.length +"本书" );
        //     for( var i = 0; i < checkboxlist.length ;i++ ){
        //         sleep(2000);  
        //         checkboxlist[i].click();
        //         sleep(2000);   
        //     }
        //     //点击删除
        //     var del_button = textStartsWith("删除").findOne(2000);
        //     sleep(2000)
        //     if(del_button){
        //         click_button( del_button.bounds() )
        //         sleep(2000)
        //         var  queren= text("确认").findOne(2000);
        //         if(queren){
        //             queren.click()
        //             toastLog("删除成功" )
        //         }
        //         else{
        //             toastLog("未找到确认按钮" )
        //         }
        //     }
        //     else{
        //         toastLog("未找到删除按钮" )
        //     }
        // }
        else {
            toastLog("没有书可删除")
        }
    }
    else {
        toastLog("没有编辑按钮")
    }
    return 0
}

//看书
function handle_watch_book() {
    var thread_1 = threads.start(function () {
        toastLog("检查广告弹窗")
        while (true) {
            var widget = id('com.dragon.read:id/qw').findOne(2000)
            if (widget) {
                toastLog('发现会员弹窗')
                widget.click();
            }
        }
    });
    var ifbook = 0
    //先接任务找到福利按钮
    find_task_button()
    click_button(app_func.bounds.task_button)

    for (var i = 0; i < 5; i++) {
        var targetButton = auto_find_button("阅读30分钟")
        if (targetButton) {
            parent = targetButton.parent().parent();
            var tb = parent.child(1).child(0);
            toastLog("阅读30分钟任务:" + tb.text());
            if (tb.text() == "已完成") {
                return 0
            }
            else {
                ifbook = 1
                break;
            }
        }
        else {
            //关广告，回任务界面
            close_all_ad();
            find_task_button()
            click_button(app_func.bounds.task_button)
        }
    }
    if (ifbook == 0) {
        toastLog("没有发现阅读任务");
        return -1
    }

    toastLog("开始看书");
    //进入书架 点击编辑 选中所有书 点击删除
    find_book_button()
    click_button(app_func.bounds.book_button)

    var book = textContains("章").findOne(5000);
    if (book) {
        sleep(2000);
        toastLog(book.text());
        click_button(book.bounds())
        sleep(2000);
        var random_min = 31 * 60
        var random_max = 33 * 60
        var watchtime = random(random_min, random_max)
        for (var i = 0; i < watchtime; i++) {
            sleep(1000)
            if ((i + 1) % 60 == 0) {
                toastLog("已经阅读" + parseInt((i + 1) / 60) + "分钟");
            }
            //挂机无法增长时间
            if ((i + 1) % 10 == 0) {
                _left_move();
                //检查是否有弹窗广告
                if (!is_mainAct() && !is_bookAct()) {
                    try {
                        有关闭控件广告 = className("android.widget.TextView").depth("10").id("ahf").findOne(500)
                        if (有关闭控件广告) {
                            click_button(有关闭控件广告.bounds())
                        }
                    } catch (e) { }
                }

                // try{ 
                //     看视频免 = textStartsWith("看视频免").findOne(500)
                //     if( 看视频免 ){
                //         click_button(看视频免.bounds() )
                //         close_time_adv()
                //     }
                // }catch(e){}
                try {
                    id("pq").findOne(500).click()
                    toastLog("1")
                } catch (e) { }
                // try{ 
                //     不再提示 = text("不再提示").findOne(500)
                //     if(不再提示){
                //         click_button( 不再提示.bounds() )
                //     }
                //     toastLog("2")
                // }catch(e){}
                //看视频再领    弹窗
            }
        }
        toastLog("看书任务完成");
        thread_1.interrupt();
    }
    else {
        toastLog("没有书可读")
    }

    return
}

function handle_watch_video_task() {
    toastLog("开始做看视频任务");
    //先接任务找到福利按钮
    find_task_button()
    click_button(app_func.bounds.task_button)
    var suctimes = 0;
    //进入任务页面寻找入口
    //检查还可以完成几次
    var parent = null;
    var targetButton = null;
    for (var i = 0; i < 5; i++) {
        targetButton = auto_find_button("看视频赚")
        if (targetButton) {
            parent = targetButton.parent();
            break;
        }
        else {
            //关广告，回任务界面
            close_all_ad();
            find_task_button()
            click_button(app_func.bounds.task_button)
        }
    }

    var tb = parent.child(4);
    var dotimes = 0 //准备观看次数
    if (tb) {
        var str = tb.text()
        toastLog(str);
        var index = str.indexOf("/")
        var num2 = str[index - 1]
        var num1 = str[index - 2]
        if (num2 == '0' && num1 == '1') {
            dotimes = 0
            toastLog("看视频任务已完成");
            return 0
        }
        else {
            dotimes = 10 - num2
        }
    }

    toastLog("剩余" + dotimes + "次看视频任务");

    for (var j = 0; j < dotimes; j++) {
        var logmsg = "第:" + (j + 1) + "次看视频任务开始..."
        sleep(3000)
        toastLog(logmsg);
        if (targetButton) {
            targetButton.click();
            if (close_time_adv() >= 0) {
                suctimes++;
                continue;
            }
            else {
                toastLog("关闭视频广告失败");
                return -1
            }
        }
    }
    if (suctimes == 0) {
        return -1
    }

    toastLog("看视频任务成功次数:", suctimes);

    return suctimes
}

function close_time_adv() {
    //总时间为 广告时间X单次寻找等待时间
    let 广告时间 = 30
    for (var i = 0; i < 广告时间; i++) {
        sleep(1000)
        //每5秒检查一次关闭按钮是否出现
        if ((i + 1) % 5 == 0) {
            //寻找时间到的线索
            if (find_end_state_button() >= 0 ||
                find_X_adv_button() >= 0) {

                继续观看 = find_button_by_textStartsWith("继续观看")
                if (继续观看) {
                    click_button(继续观看.bounds())
                    continue
                }
            }
            if (is_mainAct() || is_bookAct()) { //任务act 也应该视为广告已不存在
                return 0
            }
        }
    }
    return -1;
}

//寻找 没有倒计时存在后  back
function find_end_state_button() {
    var back_button = text("点击重播").findOne(1000)
    var cli_button = null
    if (back_button) {
        toastLog("发现关闭时机");
        back()
        return 0
    }//出现刚开始是关闭广告过两秒才开始倒计时的情况
    else if (cli_button = text("关闭广告").findOne(1000)) {
        sleep(3000)
        cli_button = text("关闭广告").findOne(1000)
        if (cli_button) {
            toastLog("关闭广告");
            click_button(cli_button.bounds())
            return 0
        }
    }
    else {
        //寻找关闭按钮
        关闭 = text("关闭").findOne(1000)
        if (关闭) { //有假关闭按钮统一back
            back()
            return 0
        }
    }
    return -1
}

function find_X_adv_button() {
    //点击下载出现的时候   倒计时控件不存在的时候
    var close =
        id("tt_video_ad_close").findOne(1000) ||
        id("tt_video_ad_close_layout").findOne(1000)
    if (close) {
        toastLog("发现关闭按钮");
        click_button(close.bounds());
        return 0
    }
    else {
        //支付宝三连
        //弹支付宝   取消安装   关闭按钮
        try {
            取消 = text("取消").findOne(500)
            if (取消) {
                click_button(取消.bounds())
                toastLog("11")
            }
        } catch (error) {
        }
        try {
            关闭 = text("关闭").findOne(500)
            if (关闭) {
                click_button(关闭.bounds())
                toastLog("22")
            }
        } catch (error) {
        }
        try {//支付宝
            if (currentPackage() != config.packageName) {
                sleep(2000)
                back()
                sleep(2000)
                继续 = textStartsWith("继续").findOne(1000)
                toastLog("33")
                if (继续) {
                    click_button(继续.bounds())
                    toastLog("44")
                }
            }
        } catch (error) {
        }
    }
    return -1
}

//向下滑
function _down_move() {
    console.log("向下滑动")
    let x = device.width / 2
        , t = device.height * 4 / 5
        , b = device.height * 1 / 5
    sml_move(x, b, x, t, 200 + random(50, 100));
    sleep(1000);
}
//向上滑
function _up_move() {
    console.log("向上滑动")
    let x = device.width / 2
        , t = device.height * 1 / 5
        , b = device.height * 4 / 5
    sml_move(x, b, x, t, 200 + random(50, 100));
    sleep(1000);
}

//向左滑
function _left_move() {
    console.log("向左滑动")
    let y = device.height / 2
        , t = device.width * 1 / 5
        , b = device.width * 4 / 5
    sml_move(b, y, t, y, 200 + random(50, 100));
    sleep(1000);
}

//向右滑
function _rigth_move() {
    console.log("向右滑动")
    let y = device.height / 2
        , t = device.width * 4 / 5
        , b = device.width * 1 / 5
    sml_move(b, y, t, y, 200 + random(50, 100));
    sleep(1000);
}

function sml_move(qx, qy, zx, zy, time) {
    //仿真随机带曲线滑动: qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
    var xxy = [time];
    var point = [
        { "x": qx, "y": qy },
        { "x": random(qx - 100, qx + 100), "y": random(qy, qy + 50) },
        { "x": random(zx - 100, zx + 100), "y": random(zy, zy + 50) },
        { "x": zx, "y": zy }
    ];
    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]
        xxy.push(xxyy);
    }
    gesture.apply(null, xxy);
}

function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = { "x": 0, "y": 0 };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
};

//自动调整按钮位置并点击  适用于在屏幕外无法点击的按钮
function auto_find_button(textstr) {
    var targetbutton = find_button_by_textStartsWith(textstr)
    var trytimes = 3
    if (targetbutton) {
        toastLog("device.width:" + device.width + "device.height:" + device.height)
        toastLog("x:" + targetbutton.bounds().centerX() + "y:" + targetbutton.bounds().centerY())
        //至少把空间拖到 屏幕 device.height * 1/5   至  device.height * 4/5 之间
        while (targetbutton.bounds().centerY() > device.height * 4 / 5) {
            var uptimes = Math.ceil((targetbutton.bounds().centerY() - device.height * 4 / 5) / (device.height * 4 / 5 - device.height * 1 / 5))
            toastLog("需要滑动调整位置次数:" + uptimes)
            for (var i = 0; i < uptimes; i++) {
                _up_move()
                sleep(2000)
            }
            targetbutton = find_button_by_textStartsWith(textstr)
            trytimes--
            if (trytimes <= 0) {
                break
            }
        }

        while (targetbutton.bounds().centerY() < device.height * 1 / 5) {
            var uptimes = Math.ceil((device.height * 1 / 5 - targetbutton.bounds().centerY()) / (device.height * 4 / 5 - device.height * 1 / 5))
            toastLog("需要滑动调整位置次数:" + uptimes)
            for (var i = 0; i < uptimes; i++) {
                _down_move()
                sleep(2000)
            }
            targetbutton = find_button_by_textStartsWith(textstr)
            trytimes--
            if (trytimes <= 0) {
                break
            }
        }

        targetbutton = find_button_by_textStartsWith(textstr)
        if (targetbutton) {
            toastLog("找到按钮:" + textstr)
        }
        else {
            toastLog("没找到按钮:" + textstr)
        }
    }
    return targetbutton
}

function handle_watch_vid_single_task() {
    toastLog("看视频再奖励任务");
    sleep(3000)
    //签到成功触发看视频再奖励按钮
    var strlist = ["看视频再领", "看视频领取"]
    var 看视频再奖励 = null
    for (var i = 0; i < strlist.length; i++) {
        看视频再奖励 = find_button_by_textStartsWith(strlist[i])
        if (看视频再奖励) {
            break;
        }
    }
    if (看视频再奖励) {
        看视频再奖励.click()
        //   click_button( 看视频再奖励.bounds() )
        if (close_time_adv() >= 0) {
            toastLog("签到视频奖励完成")
        }
        else {
            toastLog("签到视频奖励失败")
            return -1
        }
    }
    else {
        //看视频再奖励流程失败
        toastLog("看视频再奖励流程失败");
        return -1
    }
    return 0
}

app_func.main = function () {
    console.log("start...");
    var open_app = launchAppEnhance(app_func.appName);

    if (open_app) {
        sleep(5 * 1000)
        toastLog("启动成功")
        close_all_ad()  //加载慢了会判断不在主界面
        //寻找任务按钮触发签到
        find_task_button()
        click_button(app_func.bounds.task_button)

        //签到成功触发看视频再奖励按钮
        if (handle_watch_vid_single_task() < 0) {
            clean_obs()
        }

        //顺手开宝箱
        sleep(2000)
        if (open_baoxiang() >= 0) {
            //点击看视频领取
            if (handle_watch_vid_single_task() < 0) {
                clean_obs()
            }
        }

        //上架书任务
        if (handle_goto_book_task() < 0) {
            clean_obs()
        }

        //做看视频任务
        if (handle_watch_video_task() < 0) {
            clean_obs()
        }

        //阅读任务 直接听书  听书有会员限制
        if (handle_watch_book() < 0) {
            clean_obs()
        }

        toastLog("任务已执行完毕")
    }
    else {
        console.log("app启动失败");
    }

    console.log("end...");
}

try {
    app_func.main();
} catch (error) {
    toastLog(error)
}

var storage = storages.create("xiangxiangyuedu");
storage.put("script_running_status", "end");